package com.framework.controller;

import com.framework.entity.sys.SysAgent;
import com.framework.entity.tran.AliPayerAgent;
import com.framework.exception.util.RespResult;
import com.framework.service.AgentInfoService;
import com.framework.service.AgentService;
import com.framework.service.AliPayerAgentService;
import com.framework.service.TreeAgentService;
import com.framework.view.AgentVerifiedForm;
import com.framework.view.AgentView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 代理商模块
 *
 * @author ck
 * @date 2018/8/30 10:51
 */
@RestController
@Api(value="代理商模块",tags={"代理商模块"})
@RequestMapping(value = "sys")
public class AgentController {
	//代理商基础信息
	@Autowired
	private AgentService agentService;
	//代理商基础信息
	@Autowired
	private AgentInfoService agentInfoService;
	@Autowired
	private TreeAgentService treeAgentService;
	@Autowired
	private AliPayerAgentService aliPayerAgentService;
	/**
	 * 功能描述: 根据token获取当前人登录信息
	 * @param token 登录所需的token
	 */
	@GetMapping("user/getAgentByToken")
	@ApiOperation("根据token获取代理商信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true)
	})
	public RespResult getAgentInfoByToken(String token) {
		Map<String,Object> map = agentService.getAgentByToken(token);
		return RespResult.ok(map);
	}

	/**
	 * 功能描述: 注册
	 * @param phone 手机号码
	 * @param password 密码
	 * @param confPassword 确认密码
	 * @param smsCode 注册短信
	 * @param pid  推荐码
	 * @param name 昵称
	 */
	@PostMapping("register")
	@ApiOperation("注册")
	@ApiImplicitParams({
			@ApiImplicitParam(name="phone",value="手机号码",dataType="string", paramType = "query" ,required = true),
			@ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="pid",value="推荐码",dataType="Long", paramType = "query",required = true),
			@ApiImplicitParam(name="name",value="真实姓名",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="deptId",value="渠道id",dataType="Long", paramType = "query",required = true)
	})
	public RespResult register(String phone,String password,String confPassword,String smsCode,Long pid,String name,Long deptId) {
		agentService.register(phone,password,confPassword,smsCode,pid,name,deptId);
		return RespResult.ok();
	}

	/**
	 * 找回登录密码
	 * @param phone 手机号码
	 * @param password 密码
	 * @param confPassword 确认密码
	 * @param smsCode 短信验证码
	 */
	@PostMapping("loginForgetPassword")
	@ApiOperation("找回登录密码")
	@ApiImplicitParams({
			@ApiImplicitParam(name="phone",value="手机号码",dataType="string", paramType = "query" ,required = true),
			@ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="smsCode",value="验证码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name = "deptId",value="渠道id",dataType ="Long",paramType = "query",required = false)
			})
	public RespResult forgetPassword(String phone,String password,String confPassword,String smsCode,Long deptId) {
		agentService.forgetPassword(phone,password,confPassword,smsCode,deptId);
		return RespResult.ok();
	}

	/**
	 * 找回交易密码
	 * @param password 密码
	 * @param confPassword 确认密码
	 * @param token token
	 * @param vCode 手机验证码
	 */
	@PostMapping("user/payForgetPassword")
	@ApiOperation("找回交易密码")
	@ApiImplicitParams({
			@ApiImplicitParam(name="password",value="密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="confPassword",value="确认密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="vCode",value="验证码",dataType="string", paramType = "query",required = true)
	})
	public RespResult payForget(String password,String confPassword,String token,String vCode) {
		agentInfoService.updateTaskPassword(password,confPassword,token,vCode);
		return RespResult.ok();
	}

	/**
	 * 进入提现页面
	 * @param token 当前登录人token
	 */
	@GetMapping("user/withdrawInfo")
	@ApiOperation("进入提现页面")
	public RespResult withdrawInfo(String token) {
		Map<String,Object> map = agentService.withdrawInfo(token);
		return RespResult.ok(map);
	}

	/**
	 * 提现
	 * @param password 密码
	 * @param money  提现金额
	 * @param token 登录token
	 * @param bankNo 银行卡号
	 */
	@PostMapping("user/takeMoney")
	@ApiOperation("提现")
	@ApiImplicitParams({
			@ApiImplicitParam(name="password",value="提现密码",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="money",value="提现金额",dataType="BigDecimal", paramType = "query",required = true),
			@ApiImplicitParam(name="bankNo",value="银行卡号",dataType="string", paramType = "query",required = false),
	})
	public RespResult takeMoney(String token,String password,BigDecimal money,String bankNo) {
		agentInfoService.takeMoney(token,password,money,bankNo);
		return RespResult.ok();
	}

	/**
	 * 实名认证
	 * @param form 表单
	 * @param token 登录token
	 */
	@PostMapping("user/agentVerified")
	@ApiOperation("实名认证")
	@ApiImplicitParams({
			@ApiImplicitParam(name="vCode",value="手机验证码",dataType="string", paramType = "query",required = true)
	})
	public RespResult agentVerified(AgentVerifiedForm form, String token) {
		agentService.agentVerified(form,token);
		return RespResult.ok();
	}

	/**
	 * 查询下级代理商列表信息
	 * @param token token
	 * @return
	 */
	@GetMapping("user/querySubAgentList")
	@ApiOperation("查询下级代理商列表信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="grade",value="等级高低（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true)
	})
	public RespResult querySubAgentList(String token,String grade) {
		List<AgentView> list = treeAgentService.querySubAgentList(token,grade);
		return RespResult.ok().put("list",list);
	}


	/**
	 * 合伙人列表明细
	 * @param token
	 * @param partnerCount
	 * @param thisMonthAmount
	 * @param merchantCount
	 * @return
	 */
	@GetMapping("user/queryPartnerMap")
	@ApiOperation("合伙人列表明细")
	@ApiImplicitParams({
			@ApiImplicitParam(name="partnerCount",value="合伙人数量（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="thisMonthAmount",value="本月交易总额（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="merchantCount",value="商户数量（ASC:升序 DESC：降序 ）",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="realName",value="是否实名 (已认证 未认证 slient)",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="name",value="姓名",dataType="string", paramType = "query",required = true)
	})
	public RespResult queryPartnerMap(String token,String partnerCount,String thisMonthAmount,String merchantCount,String realName,String name) {
		Map<String,Object> map = agentService.queryPartnerMap(token,partnerCount,thisMonthAmount,merchantCount,realName,name);
		return RespResult.ok(map);
	}


	/**
	 * 查询自己的下级代理商信息
	 * @param token
	 * @return
	 */
	@GetMapping("user/queryAgentByPid")
	@ApiOperation("查询自己的下级代理商信息")
	public RespResult queryAgentByPid(String token) {
		List<SysAgent> list = agentService.queryAgentByPid(token);
		return RespResult.ok().put("list",list);
	}

	/**
	 * 根据手机号码越级分配查询
	 * @param token
	 * @param phone
	 * @return
	 */
	@GetMapping("user/queryOverByPhone")
	@ApiOperation("根据手机号码越级分配查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name="phone",value="手机号码",dataType="string", paramType = "query",required = true)
	})
	public RespResult queryOverByPhone(String token,String phone) {
		List<SysAgent> list = agentService.queryOverByPhone(token,phone);
		return RespResult.ok().put("list",list);
	}

	/**
	 * 查询代理商等级
	 * @param token
	 * @return
	 */
	@GetMapping("user/queryLv")
	@ApiOperation("查询代理商等级")
	public RespResult queryLv(String token) {
		Integer lv = agentService.queryLv(token);
		return RespResult.ok().put("lv",lv);
	}

	/**
	 * 合伙人详情
	 * @param token
	 * @param types
	 * @param time
	 * @return
	 */
	@GetMapping("user/queryAgentInfo")
	@ApiOperation("合伙人详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name="types",value="日维度(day) 月维度(month)",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="time",value="时间维度 月(yyyy-mm) 日(yyyy-mm-dd)",dataType="string", paramType = "query",required = true),
			@ApiImplicitParam(name="agentId",value="查询的代理商id",dataType="int", paramType = "query",required = true)
	})
	public RespResult queryAgentInfo(String token,String types,String time,Long agentId) {
		Map<String,Object> map = agentService.queryAgentInfo(token,types,time,agentId);
		return RespResult.ok(map);
	}

	/**
	 * 上传图片
	 * @param token
	 * @param img 图片地址
	 * @return
	 */
	@PostMapping("user/updateHeadImg")
	@ApiOperation("修改头像图片")
	@ApiImplicitParams({
			@ApiImplicitParam(name="img",value="头像img图片地址",dataType="string", paramType = "query",required = true)
	})
	public RespResult updateHeadImg(String token,String img) {
		agentService.updateHeadImg(token,img);
		return RespResult.ok();
	}

	/**
	 * 根据token 获取支付宝信息
	 * @param token
	 * @return
	 */
	@GetMapping("user/queryAliAccount")
	@ApiOperation("查询个人的支付宝信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true)
	})
	public RespResult queryAliAccount(String token){
		AliPayerAgent aliPayerAgent=aliPayerAgentService.queryAliAccount(token);
		return RespResult.ok().put("aliAccount",aliPayerAgent);
	}

	/**
	 * 绑定支付宝账号
	 * @return
	 */
	@PostMapping("user/bindAliAccount")
	@ApiOperation("绑定支付宝账号")
	@ApiImplicitParams({
			@ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true),
			@ApiImplicitParam(name="name",value="支付宝姓名",dataType ="string",paramType = "query",required = true),
			@ApiImplicitParam(name="aliAccount",value="支付宝账号",dataType ="string",paramType = "query",required = true)
	})
	public RespResult bindAliAccount(String token,String name,String aliAccount){
		aliPayerAgentService.bindAliAccount(token,name,aliAccount);
		return RespResult.ok();
	}

	/**
	 * 修改支付宝账号
	 * @return
	 */
	@PostMapping("user/updateAliAccount")
	@ApiOperation("修改支付宝账号信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name="token",value="登录所产生的token",dataType ="string",paramType = "query",required = true),
			@ApiImplicitParam(name="name",value="支付宝姓名",dataType ="string",paramType = "query",required = true),
			@ApiImplicitParam(name="aliAccount",value="支付宝账号",dataType ="string",paramType = "query",required = true)
	})
	public RespResult updateAliAccount(String token,String name,String aliAccount){
		aliPayerAgentService.updateAliAccount(token,name,aliAccount);
		return RespResult.ok();
	}
}
